package com.example.myapplicationndk.mvp;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplicationndk.databinding.ActivityMvpBinding;
import com.example.myapplicationndk.mvp.base.BaseView;
import com.example.myapplicationndk.mvp.bean.User;
import com.example.myapplicationndk.mvp.presenter.MainPresenter;

public class MvpActivity extends AppCompatActivity implements BaseView, View.OnClickListener {

    private MainPresenter mMainPresenter;
    private ProgressDialog dialog;
    private ActivityMvpBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMvpBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        initView();
        initListener();
        mMainPresenter = new MainPresenter();
        mMainPresenter.attachView(this);
    }


    /**
     * 一些初始化，这里为ProgressDialog的初始化
     */
    private void initView() {
        dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("正在搜索中");
    }

    private void initListener() {
        mBinding.searchBtn.setOnClickListener(this);
    }


    @Override
    public void showProgressDialog() {
        dialog.show();
    }

    @Override
    public void hideProgressDialog() {
        dialog.dismiss();
    }

    @Override
    public void showText(User userbean) {
//        String temp = getResources().getString(R.string.user_format);
//        String str = String.format(temp, userbean.getLogin(), userbean.getName(), userbean.getFollowers(), userbean.getFollowing());
        mBinding.tv.setText(userbean.getName());
    }

    @Override
    public void showErrorMessage(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMainPresenter != null)
            mMainPresenter.detachView();
    }

    @Override
    public void onClick(View v) {
        mMainPresenter.searchUser(mBinding.edText.getText().toString());
    }
}